package com.school.controller.admin;

import com.school.common.IndexConfigTypeEnum;
import com.school.common.ServiceResultEnum;
import com.school.entity.IndexConfig;
import com.school.service.IndexConfigService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/admin")
public class IndexConfigController {
    @Resource
    private IndexConfigService indexConfigService;
    @GetMapping("/indexConfigs")
    public String indexConfigs(HttpServletRequest request, @RequestParam("configType")int configType){
        IndexConfigTypeEnum indexConfigTypeEnum = IndexConfigTypeEnum.getIndexConfigTypeEnumByType(configType);
        if(indexConfigTypeEnum == null){
            return "error/error_400";
        }
        request.setAttribute("configType",configType);
        request.setAttribute("path",indexConfigTypeEnum.getName());
        return "admin/tea_shop_index_config";
    }

    @GetMapping("/indexConfigs/list")
    @ResponseBody
    public Result list(@RequestParam Map<String,Object> params){
        if(StringUtils.isEmpty(params.get("page"))||
                StringUtils.isEmpty(params.get("limit"))){
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        PageResult pageResult = indexConfigService.getConfigsPage(pageQueryUtil);
        return ResultGenerator.genSuccessResult(pageResult);
    }

    @PostMapping("/indexConfigs/update")
    @ResponseBody
    public Result update(@RequestBody IndexConfig indexConfig) {
        // 参数校验
        if (Objects.isNull(indexConfig.getConfigType()) ||
                StringUtils.isEmpty(indexConfig.getConfigName()) ||
                Objects.isNull(indexConfig.getGoodsId()) ||
                Objects.isNull(indexConfig.getConfigRank()) ||
                Objects.isNull(indexConfig.getConfigId())) {
            return ResultGenerator.genFailResult("参数异常！");
        }

        // 调用服务层方法进行更新操作
        String result = indexConfigService.updateIndexConfig(indexConfig);

        // 根据服务层返回的结果生成响应
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    @PostMapping("/indexConfigs/save")
    @ResponseBody
    public Result save(@RequestBody IndexConfig indexConfig) {
        // 参数校验
        if (Objects.isNull(indexConfig.getConfigType()) ||
                StringUtils.isEmpty(indexConfig.getConfigName()) ||
                Objects.isNull(indexConfig.getGoodsId()) ||
                Objects.isNull(indexConfig.getConfigRank())) {
            return ResultGenerator.genFailResult("参数异常！");
        }

        // 调用服务层方法进行保存操作
        String result = indexConfigService.saveIndexConfig(indexConfig);
        System.out.println(result);

        // 根据服务层返回的结果生成响应
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }



}